package results;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class ClusterVector {
	
	private SortedMap<Integer, ArrayList<String>> CentroidMap;
	
	
	public ClusterVector() {
		CentroidMap = new TreeMap<Integer,ArrayList<String>>();
	}
	
	/**
	 * 
	 * @param dir
	 * @throws Exception
	 */
	public void printCenters(File inFile, File outFile) throws Exception {

		BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
			
		BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "UTF-8"));
		
		String line1, line2;
		
		// read characters
		while (input.read() != -1)
		{
			line1 = input.readLine();
			line2 = input.readLine();
			StringTokenizer st1 = new StringTokenizer(line1, " "); 
			StringTokenizer st2 = new StringTokenizer(line2, " "); 
			while(st1.hasMoreTokens()) { 
				String value = st1.nextToken(); 
				int key = Integer.valueOf(st2.nextToken()); 
				ArrayList<String> aList = new ArrayList<String>();
				if (CentroidMap.containsKey(key)) {		
					aList = CentroidMap.get(key);	
				} 
				aList.add(value);
				CentroidMap.put(key, aList);
			}
		}
		List<Integer> listKey = new ArrayList<Integer>(CentroidMap.keySet());
		
		for (int t = 0; t < CentroidMap.size(); t++) {
			output.append(listKey.get(t) + "\n");
			output.append(CentroidMap.get(listKey.get(t)).toString() + "\n");
		}
		input.close();
		output.close();
	}
}